namespace SimpleWebServices.Configuration
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    [XmlRoot("files")]
    public sealed class FileSettingCollection : Collection<FileSetting>, IXmlSerializable
    {
        public FileSetting this[string path]
        {
            get
            {
                FileSetting result = null;

                if (!string.IsNullOrEmpty(path))
                {
                    result = this
                        .Where(x => path.Equals(x.Path, StringComparison.OrdinalIgnoreCase))
                        .FirstOrDefault();
                }

                return result;
            }
        }

        public bool Contains(string path)
        {
            return null != this[path];
        }

        XmlSchema IXmlSerializable.GetSchema()
        {
            throw new NotSupportedException();
        }

        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (null == reader)
            {
                throw new ArgumentNullException("reader");
            }

            if (reader.IsEmptyElement)
            {
                reader.Read();
            }
            else
            {
                string name = reader.Name;
                string group = null;
                string condition = null;
                string control = null;
                string duration = null;
                string type = null;
                while (true)
                {
                    if (XmlNodeType.EndElement == reader.NodeType && reader.Name == name)
                    {
                        reader.Read();
                        break;
                    }
                    else if ("file".Equals(reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        FileSetting obj = reader.ReadOuterXml().Deserialize<FileSetting>();
                        obj.ContentType = string.IsNullOrEmpty(obj.ContentType) ? type : obj.ContentType;
                        obj.CacheCondition = string.IsNullOrEmpty(obj.CacheCondition) ? condition : obj.CacheCondition;
                        obj.CacheControl = string.IsNullOrEmpty(obj.CacheControl) ? control : obj.CacheControl;
                        obj.DurationValue = string.IsNullOrEmpty(obj.DurationValue) ? duration : obj.DurationValue;

                        this.Add(obj);
                    }
                    else
                    {
                        if (!reader.IsEmptyElement)
                        {
                            if (XmlNodeType.EndElement == reader.NodeType && reader.Name == group)
                            {
                                group = null;
                                control = null;
                                condition = null;
                                duration = null;
                                type = null;
                            }
                            else
                            {
                                group = reader.Name;
                                condition = reader.GetAttribute("cache-condition");
                                control = reader.GetAttribute("cache-control");
                                type = reader.GetAttribute("content-type");
                                duration = reader.GetAttribute("duration");
                            }
                        }

                        reader.Read();
                    }
                }
            }
        }

        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (null == writer)
            {
                throw new ArgumentNullException("writer");
            }

            foreach (FileSetting setting in this)
            {
                writer.WriteRaw(setting.Serialize().CreateNavigator().OuterXml);
            }
        }
    }
}